﻿using System.IO;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Windows.Forms;

namespace Twillo.Core.Settings
{
    public static class SettingsManager
    {

        public const string ConsumerKey = "hfrvNnFTyadI1panxHhsyg";
        public const string ConsumerSecret = "zO0GtJeXzSomdQ5hvs1h2xSLPY0m5XpyWWt7kN0";
        public const string APIKey = "hfrvNnFTyadI1panxHhsyg";

        public static string Username
        {
            get { return GetEncryptedValue("Username"); }
            set { SetEncryptedValue("Username", value); }
        }

        public static Int32 RefreshIntervalSeconds
        {
            get
            {
                Int32 value = 60;
                if (!Int32.TryParse(GetValue("RefreshIntervalSeconds"), out value)) { value = 60; }
                return value;
            }
            set { SetValue("RefreshIntervalSeconds", value.ToString()); }
        }

        public static decimal UserId
        {
            get
            {
                decimal value = 0;
                if (!decimal.TryParse(GetValue("UserId"), out value)) { value = 0; }
                return value;
            }
            set { SetValue("UserId", value.ToString()); }
        }

        public static Int32 TimeLineCount
        {
            get
            {
                Int32 value = 0;
                if (!Int32.TryParse(GetValue("TimeLineCount"), out value)) { value = 0; }
                return value;
            }
            set { SetValue("TimeLineCount", value.ToString()); }
        }

        public static string AccessToken
        {
            get { return GetValue("AccessToken"); }
            set { SetValue("AccessToken", value); }
        }

        public static string AccessTokenSecret
        {
            get { return GetValue("AccessTokenSecret"); }
            set { SetValue("AccessTokenSecret", value); }
        }

        public static string Password
        {
            get { return GetEncryptedValue("Password"); }
            set { SetEncryptedValue("Password", value); }
        }

        public static string ProxyUsername
        {
            get { return GetEncryptedValue("ProxyUsername"); }
            set { SetEncryptedValue("ProxyUsername", value); }
        }

        public static bool UseSSL
        {
            get
            {
                bool value = false;
                if (!bool.TryParse(GetValue("UseSSL"), out value)) { value = false; }
                return value;
            }
            set { SetValue("UseSSL", value.ToString()); }
        }

        public static double FormOpacity
        {
            get
            {
                double value = 100;
                if (!double.TryParse(GetValue("FormOpacity"), out value)) { value = 100; }
                return value / 100;
            }
            set { SetValue("FormOpacity", (value * 100).ToString()); }
        }

        public static string ProxyPassword
        {
            get { return GetEncryptedValue("ProxyPassword"); }
            set { SetEncryptedValue("ProxyPassword", value); }
        }

        public static System.Net.NetworkCredential ProxyCredentials
        {
            get { return new System.Net.NetworkCredential(ProxyUsername, ProxyPassword); }
        }

        public static System.Net.NetworkCredential AccountCredentials
        {
            get { return new System.Net.NetworkCredential(Username, Password); }
        }

        private static string GetValue(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key];
        }

        private static void SetValue(string key, string value)
        {
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            AppSettingsSection section = config.GetSection("appSettings") as AppSettingsSection;
            bool found = false;

            foreach (string s in section.Settings.AllKeys)
            {
                if (s.ToLower() == key.ToLower())
                {
                    section.Settings[s].Value = value;
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                section.Settings.Add(new KeyValueConfigurationElement(key.ToLower(), value));
            }

            config.Save();
        }

        private static void SetEncryptedValue(string key, string value)
        {
            SetValue(key, EncryptionProvider.Encode1(value, EncryptionProviderType.TripleDES));
        }
        private static string GetEncryptedValue(string key)
        {
            return EncryptionProvider.Decode1(GetValue(key), EncryptionProviderType.TripleDES);
        }

    }
}
